package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGenerateDialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkCustomTemplet.jsonHelp
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkCustomTemplet.CustomQuesTypeInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletSelectedTypeListInfor
import com.lancoo.znbkxx.teacher.widget.MyLayoutManager
import kotlinx.android.synthetic.main.aibk_dialog_template_questype_choose.*

/*
* 模板题型选择弹窗
* */
class DialogTempletQuesTypeChoose(
        context: Context?,
        val activity: AppCompatActivity,
        val templetSelectedTypeListInfor: TempletSelectedTypeListInfor
) : Dialog(context!!, R.style.CustomDialog), View.OnClickListener {

    private var currentPosition = 0
    private var currentBigQuesCount = 1
    private var currentSmallItemCount = 0
    private var currentEachScore = 0.0

    //以下是用于组装试题模型的变量
    private var aibk_genreId = 0
    private var aibk_genreName = ""
    private var aibk_typeNo = ""
    private var aibk_typeName = ""
    private var aibk_specialFeatureCodes = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.aibk_dialog_template_questype_choose)
        window!!.setGravity(Gravity.BOTTOM) //设置弹窗右对齐
        window!!.setWindowAnimations(R.style.MyDialogBottomTranslate) //设置动画效果

        val wl = window!!.attributes
        //以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        onWindowAttributesChanged(wl)
        setCanceledOnTouchOutside(false)
        //初始化界面数据
        initView()
    }

    /**
     * 初始化界面控件的显示数据
     */
    private fun initView() {
        //关闭dialog
        IvCloseDialog.setOnClickListener(this)
        //新增按钮
        BtnQuesAdd.setOnClickListener(this)

        //显示题型选择区域
        val layout = MyLayoutManager()
        layout.isAutoMeasureEnabled = true //必须，防止recyclerview高度为wrap时测量item高度0
        RvQuesTypeChoose.layoutManager = layout
        val adapterTempletQuesType = AibkTempletQuesTypeAdapter(activity, templetSelectedTypeListInfor)
        adapterTempletQuesType.setClickDialogListener(object :AibkTempletQuesTypeAdapter.OnClickQuesTypeListener{
            override fun onClickQuesType(pos:Int) {
                val indexB = templetSelectedTypeListInfor[pos].ItemCountCheckedPosition
                showQuesTypeInfor(pos,indexB)
            }
        })
        RvQuesTypeChoose.adapter = adapterTempletQuesType

        //显示题型对应的试题内容设置
        adapterTempletQuesType.perClickWhichButton(0)
        showQuesTypeInfor(0,0)

    }

    private fun showQuesTypeInfor(indexA:Int,indexB:Int){
        aibk_genreId = templetSelectedTypeListInfor[indexA].GenreID
        aibk_genreName = templetSelectedTypeListInfor[indexA].GenreName
        aibk_typeNo = templetSelectedTypeListInfor[indexA].TypeNo
        aibk_typeName = templetSelectedTypeListInfor[indexA].TypeName

        currentPosition = indexA
        currentBigQuesCount = templetSelectedTypeListInfor[indexA].QuesCount
        currentSmallItemCount = templetSelectedTypeListInfor[indexA].CountList[0].ItemNum
        currentEachScore = templetSelectedTypeListInfor[indexA].CountList[0].ItemScore

        //显示大题数量
        IvBigLessen.setOnClickListener(this)
        IvBigAdd.setOnClickListener(this)
        EdtBigQuesCount.setText(currentBigQuesCount.toString())
        refreshBigQuesCountView()
        //显示小题分值
        IvSmallLessen.setOnClickListener(this)
        IvSmallAdd.setOnClickListener(this)
        EdtSmallQuesScore.setText(currentEachScore.toString())
        refreshSmallQuesScoreView()

        //显示附加项内容
        val layout1 = MyLayoutManager()
        layout1.isAutoMeasureEnabled = true //必须，防止recyclerview高度为wrap时测量item高度0
        RvAttached.layoutManager = layout1
        val adapterTempletAttached = AibkTempletAttachedAdapter(activity, templetSelectedTypeListInfor,indexA,indexB)
        adapterTempletAttached.setClickAttachedListener(object :AibkTempletAttachedAdapter.OnClickAttachedListener{
            override fun onClickAttached(specialFeatureCodes: String) {
                aibk_specialFeatureCodes = specialFeatureCodes
            }

        })
        RvAttached.adapter = adapterTempletAttached

        //显示试题小题数的区域
        val layout = MyLayoutManager()
        layout.isAutoMeasureEnabled = true      //必须，防止recyclerview高度为wrap时测量item高度0
        RvItemCount.layoutManager = layout
        val adapterTempletItemCount = AibkTempletItemCountAdapter(activity, templetSelectedTypeListInfor,indexA)
        adapterTempletItemCount.setClickItemCountListener(object :AibkTempletItemCountAdapter.OnClickItemCountListener{
            override fun onClickItemCount(indexA: Int,indexB: Int) {
                currentSmallItemCount = templetSelectedTypeListInfor[indexA].CountList[indexB].ItemNum
                //刷新附加项内容
                adapterTempletAttached.refreshIndexB(indexB)
            }

        })
        RvItemCount.adapter = adapterTempletItemCount

    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.IvCloseDialog ->{
                dismiss()
                clickAddListener!!.onClickCloseChoose()
            }
            R.id.BtnQuesAdd ->{
                dismiss()
                Log.e("ppsspp","==============currentSmallItemCount============="+currentSmallItemCount)
                val strSingleCustomQuesInfor =  Gson().toJson(jsonHelp.getSingleCustomQues(
                        aibk_typeNo,aibk_typeName,aibk_genreId,aibk_genreName,
                        currentBigQuesCount,currentSmallItemCount,currentEachScore,
                        aibk_specialFeatureCodes
                ))
                Log.e("ppsspp","======组装的内容======="+strSingleCustomQuesInfor)
                val customQuesTypeInfor: CustomQuesTypeInfor = Gson().fromJson(strSingleCustomQuesInfor, CustomQuesTypeInfor::class.java)

                clickAddListener!!.onClickAdd(customQuesTypeInfor)
            }
            R.id.IvBigLessen ->{
                if(currentBigQuesCount == 1){
                    MyToast.showToast("最小大题数为1")
                }else {
                    currentBigQuesCount -= 1
                    templetSelectedTypeListInfor[currentPosition].QuesCount = currentBigQuesCount
                }
                refreshBigQuesCountView()
            }
            R.id.IvBigAdd ->{
                currentBigQuesCount += 1
                templetSelectedTypeListInfor[currentPosition].QuesCount = currentBigQuesCount
                refreshBigQuesCountView()
            }
            R.id.IvSmallLessen ->{
                if(currentEachScore == 0.5){
                    MyToast.showToast("单题最小分值为0.5")
                }else {
                    currentEachScore -= 0.5
                }
                templetSelectedTypeListInfor[currentPosition].CountList[0].ItemScore = currentEachScore
                refreshSmallQuesScoreView()
            }
            R.id.IvSmallAdd ->{
                currentEachScore += 0.5
                templetSelectedTypeListInfor[currentPosition].CountList[0].ItemScore = currentEachScore
                refreshSmallQuesScoreView()
            }
        }
    }

    //刷新大题数量操作区域
    private fun refreshBigQuesCountView(){
        EdtBigQuesCount.setText(currentBigQuesCount.toString())
        if(currentBigQuesCount==1){
            IvBigLessen.setImageResource(R.mipmap.aibk_icon_lessen_off)
        }else{
            IvBigLessen.setImageResource(R.mipmap.aibk_icon_lessen)
        }
    }

    //刷新小题数量操作区域
    private fun refreshSmallQuesScoreView(){
        EdtSmallQuesScore.setText(currentEachScore.toString())
        if(currentEachScore == 0.5){
            IvSmallLessen.setImageResource(R.mipmap.aibk_icon_lessen_off)
        }else{
            IvSmallLessen.setImageResource(R.mipmap.aibk_icon_lessen)
        }
    }

    private var clickAddListener: OnClickDialogListener? = null

    /*
   * 设置答题卡题号点击事件
   * */
    interface OnClickDialogListener {
        fun onClickAdd(customQuesTypeInfor: CustomQuesTypeInfor)//新增
        fun onClickCloseChoose()//关闭dialog，还原初始化数据
    }

    fun setClickDialogListener(clickAddListener: OnClickDialogListener?) {
        this.clickAddListener = clickAddListener
    }
}